<?php

class ChapterController extends Controller
{
	public function actionView($id)
	{
		$chapter = Chapter::model()->findByPk($id);
		if($chapter !== null){
			$this->render('view',array('chapter'=>$chapter));
		}
	}

	public function actionCreate($id)
	{
		$lesson = Lesson::model()->findByPk($id);
		if(!Yii::app()->user->isAdmin && !$lesson->isOwner()){
			echo 'You are not owner.';
			Yii::app()->end();
		}
		
		if(isset($_POST['createChapter'])){
			$chapter = new Chapter;
			$chapter->lesson_id = $lesson->lesson_id;
			$chapter->attributes = $_POST['createChapter'];
			if($chapter->save()){
				echo CJSON::encode(array('success'=>TRUE, 'chapter'=>$chapter->attributes));
				Yii::app()->end();
			}
			else{
				echo CJSON::encode(array('success'=>FALSE, 'errors'=>$chapter->errors));
				Yii::app()->end();
			}
		}
		
		$this->renderPartial('create',array('lesson'=>$lesson));
	}
	
	
	
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}